package com.example.spring.exercise.service.impl;

import com.example.spring.exercise.dao.RougeDao;
import com.example.spring.exercise.repository.RougeRepository;
import com.example.spring.exercise.service.RougeService;
import com.example.spring.exercise.service.dto.rouge.BrandData;
import com.example.spring.exercise.service.dto.rouge.RougeInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : zhayh
 * @date : 2021-3-31 20:13
 * @description :
 */

@Slf4j
@Service
public class RougeServiceImpl implements RougeService {
    @Resource
    private RougeDao rougeDao;
    @Resource
    private RougeRepository rougeRepository;

    @Override
    public List<BrandData> getTopBrandData() {
        return rougeDao.selectTopBrandData();
    }

    @Override
    public Page<RougeInfo> getRougeByBrandAndPrice(String brandName, Float minPrice, Float maxPrice,
                                                   Integer pageIndex, Integer pageSize) {
        return rougeDao.selectRougeByBrandAndPrice(brandName, minPrice, maxPrice, pageIndex, pageSize);
    }

    @Override
    public RougeInfo insert(RougeInfo rougeInfo) {
        return rougeRepository.save(rougeInfo);
    }

    @Override
    public Page<RougeInfo> getRougeByCountry(String country, Float minPrice, Float maxPrice,
                                             Integer page, Integer size) {
        return rougeRepository.selectByCountry(country, minPrice, maxPrice,
                PageRequest.of(page, size));


    }

    @Override
    public RougeInfo update(Long id, String rougeName) {
        int num = rougeRepository.updateRougeName(id, rougeName);
        if (num > 0) {
            return rougeRepository.findById(id).orElse(null);
        }
        return null;
    }
}


